home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 16589 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.0 KB  |  110 lines

  1. Path: erinews.ericsson.se!usenet
  2. From: Bjorn Fahller <ebcbear@ebc.ericsson.se>
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Creating an object via new with ONLY a pointer to the object
  5. Date: Thu, 11 Apr 1996 14:37:43 +0200
  6. Organization: Ericsson Business Networks AB
  7. Message-ID: <316CFD17.5656@ebc.ericsson.se>
  8. References: <4kh07v$lno@crchh327.rich.bnr.ca> <4kifrt$lk1@ubszh.fh.zh.ubs.com>
  9. NNTP-Posting-Host: ebcw050.ebc.ericsson.se
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 2.0 (X11; I; HP-UX A.09.05 9000/735)
  14.  
  15. Ian Johnston (by ubsswop) wrote:
  16. > In article <4kh07v$lno@crchh327.rich.bnr.ca>, jobell@bnr.ca (Bret Bieghler) writes:
  17. > |> An interesting problem I've come across... I was wondering if this
  18. > |> is possible:
  19. > |>
  20. > |> I have a generic CommandObject which defines several pure virtual
  21. > |> functions.  Derived from CommandObject are user commands, such
  22. > |> as ExitCommand, StatusCommand, etc.
  23. > |>
  24. > |> To process a command (currently) I do the following:
  25. > |>
  26. > |>      CommandObject* basePtr;
  27. > |>
  28. > |>      if (command == "exit")
  29. > |>      {
  30. > |>              basePtr = new ExitCommand;
  31. > |>              basePtr->implement();
  32. > |>      }
  33. > |>      else if (command == "status")
  34. > |>      {
  35. > |>              basePtr = new StatusCommand;
  36. > |>              basePtr->implement();
  37. > |>      }
  38. > |>
  39. > |> What I would LIKE to do is the the following:
  40. > |>
  41. > |>      CommandObject* basePtr =  new commandTable[command];
  42. > |>
  43. > |> where commandTable is an associative array as follows:
  44. > |>
  45. > |>      Key             Value
  46. > |>      "exit"  ExitCommand*
  47. > |>      "status"        StatusCommand*
  48. > Give each CommadObject class a *static* function:
  49. > class ExitCommand : public CommandObject
  50. > {
  51. >     // ...
  52. >     static CommandObject *makeNew();
  53. > };
  54. > CommandObject *ExitCommand::makeNew()
  55. > {
  56. >     return new ExitCommand;
  57. > }
  58. > Now you can make a table (assoc array, whatever) of strings and pointers
  59. > to these functions.
  60. > typedef CommandObject *(*MakeCmdObj)();
  61. > MakeCmdObj funcptr = commandTable[command];
  62. > CommandObject *baseptr = (*funcptr)();
  63. > Ian
  64.  
  65. You can make it even slicker by a small template idiom.
  66.  
  67. Add this:
  68.  
  69. template <class T>
  70. class creatable : public CommandObject
  71. {
  72. public:
  73.   static CommandObject* create(void);
  74. };
  75.  
  76. template <class T>
  77. CommandObject* creatable<T>::create(void)
  78. {
  79.   return new T;
  80. }
  81.  
  82. Now make your object inherit creatable, instantiated by
  83. themselves:
  84.  
  85. class ExitCommand : public creatable<ExitCommand>
  86. ...
  87.  
  88.  
  89. I'm not sure if the new rules for overloading on return type
  90. would allow for releasing creatable from CommandObject (to
  91. let you multiply inherit it instead.)
  92.  
  93. The rest of this is handled just in the example given
  94. above.
  95.    _
  96. /Bjorn.
  97. --
  98. Bjorn Fahller                  Tel: +46 8 4220898 / 
  99. NA/EBC/FNM/T                   -------------------            
  100. Ericsson Business Networks AB /     "Don't you try to outweird me"
  101. S-131 89 Stockholm/SWEDEN    /            -- Zaphod Beeblebrox
  102.